import AbsTopo from './abstopo.js'
import {LGXEnums}  from '../common/sysenum.js';

import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;
import { GlobalVar } from '../common/globalvar.js';

import { CrossModLineSvc } from '../service/crossmodlinesvc.js';

import toponode from '../common/struct/basestruct/toponode.js';

class ConnectNode extends AbsTopo.AbsTopo {
    constructor() {
        super();
        this.m_refRes = null;
        this.m_connLineList = new Array();
        this.x = 0;
        this.y = 0;
        this.type = "node";
    }

    //只修改坐标，其关联线路端点坐标由其他流程修正
    setPos(x, y) {
        this.x = x;
        this.y = y;
    }

    addConnLine(lineRes) {
        if (this.m_connLineList.indexOf(lineRes) == -1) {
            var pos1 = lineRes.getNodeInListPos(this);
            var lnConnNodeCount = lineRes.getNodeCount();
            if (pos1 == -1 && lnConnNodeCount < 2) {
                lineRes.addNode(this);
            }
            if (this.m_connLineList.indexOf(lineRes) == -1)
                this.m_connLineList.push(lineRes);
        }
    }

    clearConnLine() {
        var size = this.m_connLineList.length();
        for (var i = size - 1; i >= 0; i--) {
            var pLineSeg = this.m_connLineList[i];
            this.removeLineSeg(pLineSeg);
        }
    }

    removeLineSeg(pLineSeg) {
        var removePos = this.m_connLineList.indexOf(pLineSeg);
        if (removePos >= 0) {
            this.m_connLineList.splice(removePos, 1);
            pLineSeg.removeNode(this);
        }
    }

    setRefRes(refRes) { this.m_refRes = refRes; }
    getRefRes() { return this.m_refRes; }

    getConnLineCount() { return this.m_connLineList.length; }
    getConnLine(idx) { return this.m_connLineList[idx]; }
    
    translate(curentCnt, offsetPt,tInMulSelRegionLineLst) {
        let nodePt = { x: this.x, y: this.y };
        let size = this.m_connLineList.length;
        for (let i = 0; i < size; i++) {
            let pLineSeg = this.m_connLineList[i];
            let lineGraph = pLineSeg.getRefGraph();
            let needDragLineEnd = true;
            if (tInMulSelRegionLineLst && lineGraph && tInMulSelRegionLineLst.indexOf(lineGraph) >= 0){
                //已经在批量修改队列中的线路不需要在此进行联动处理，因为批量操作就会对它进行整体移动
                needDragLineEnd = false;
            }
            if (needDragLineEnd){
                this.transLineConn2NodePt(pLineSeg, nodePt, curentCnt, offsetPt);
            }
        }

        this.m_modifiedFlag = 1;
        this.x = curentCnt.x + offsetPt.x;
        this.y = curentCnt.y + offsetPt.y;
    }

    transLineConn2NodePt(pLineSeg, nodePt, nodeGraphCnt, offsetPt) {
        let refLineGraph = pLineSeg.getRefGraph();
        if (!refLineGraph || (refLineGraph && refLineGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type)) {
            return;
        }
        let selPosIdx = 0;
        let position = refLineGraph.getPosCopy();
        let p1 = position.getHead();
        let p2 = position.getTail();
        let dis1 = funcs.distancePoint(nodePt, p1, 1);
        let dis2 = funcs.distancePoint(nodePt, p2, 1);

        let destPt = p1;
        if (dis2 < dis1) {
            destPt = p2;
            selPosIdx = position.posList.length - 1;
        }
        if (!offsetPt.x) {
            console.log("error");
        }

        let preDestPt = { x: destPt.x, y: destPt.y };
        destPt.x = nodeGraphCnt.x + offsetPt.x;
        destPt.y = nodeGraphCnt.y + offsetPt.y;

        if (refLineGraph.visionType == LGXEnums.lineVisionType.cross) {
            refLineGraph.setPos(position);
            CrossModLineSvc.modifyEnd(refLineGraph, preDestPt, destPt,selPosIdx);
        }
        else {
            refLineGraph.setPos(position);
            refLineGraph.calScope(1);
        }
    }

    translate2DestPt(destPt,tInMulSelRegionLineLst) {
        let nodePt = { x: this.x, y: this.y };
        let size = this.m_connLineList.length;
        for (let i = 0; i < size; i++) {
            let pLineSeg = this.m_connLineList[i];
            let lineGraph = pLineSeg.getRefGraph();
            let needDragLineEnd = true;
            if (tInMulSelRegionLineLst && lineGraph && tInMulSelRegionLineLst.indexOf(lineGraph) >= 0){
                //已经在批量修改队列中的线路不需要在此进行联动处理，因为批量操作就会对它进行整体移动
                needDragLineEnd = false;
            }
            if (needDragLineEnd){
                this.transLineEndConn2NodePt(pLineSeg, nodePt,destPt);
            }
        }
        
        this.x = destPt.x;
        this.y = destPt.y;
        this.m_modifiedFlag = 1;
    }

    transLineEndConn2NodePt(pLineSeg, nodePrePt, destNodePt) {
        let refLineGraph = pLineSeg.getRefGraph();
        if (!refLineGraph || (refLineGraph && refLineGraph.m_graphType != LGXEnums.LGXGraphType.LineGraph_type)) {
            return;
        }
        let selPosIdx = 0;
        let position = refLineGraph.getPosCopy();
        let p1 = position.getHead();
        let p2 = position.getTail();
        let dis1 = funcs.distancePoint(nodePrePt, p1, 1);
        let dis2 = funcs.distancePoint(nodePrePt, p2, 1);

        let destLnEndPt = p1;
        if (dis2 < dis1) {
            destLnEndPt = p2;
            selPosIdx = position.posList.length - 1;
        }

        let preDestPt = { x: destLnEndPt.x, y: destLnEndPt.y };
        destLnEndPt.x = destNodePt.x;
        destLnEndPt.y = destNodePt.y;

        if (refLineGraph.visionType == LGXEnums.lineVisionType.cross) {
            refLineGraph.setPos(position);
            CrossModLineSvc.modifyEnd(refLineGraph, preDestPt, destLnEndPt,selPosIdx);
        }
        else {
            refLineGraph.setPos(position);
            refLineGraph.calScope(1);
        }
    }

    getPos() {
        var x = this.x;
        var y = this.y;
        return { x, y };
    }

    toInfo() {
        let info = new toponode.TopoNode();
        info.id = this.id;
        info.x = this.x;
        info.y = this.y;
        if (this.m_refRes){
            info.refID = this.m_refRes.getRefGraph().m_rid;
        }
        if (this.m_connLineList.length == 0){
            this.setUseful(0);
        }
        return info;
    }

    offset(x, y) {
        this.x += x;
        this.y += y;
    }

    getConnValidLineList() {
        let list = [];
        let size = this.m_connLineList.length;
        for (let i = 0; i < size; i++) {
            let pLineSeg = this.m_connLineList[i];
            if (pLineSeg.getUseful() != 0){
                list.push(pLineSeg);
            }
        }
       return list;
    }

    getOnlyoneConnLine(){
        if (this.m_connLineList.length == 1){
            return this.m_connLineList[0];
        }
        return null;
    }
}
export default { ConnectNode }
